package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.ProductFirst;
import cn.wolfcode.crm.mapper.ProductFirstMapper;
import cn.wolfcode.crm.mapper.SecondaryMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IProductFirstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ProductFirstServiceImpl implements IProductFirstService {
    @Autowired
    private ProductFirstMapper productFirstMapper;
    @Autowired
    private SecondaryMapper secondaryMapper;

    public ProductFirst selectByPrimaryKey(Long id) {
        return productFirstMapper.selectByPrimaryKey(id);
    }

    public List<ProductFirst> selectAll() {
        return productFirstMapper.selectAll();
    }
    public void delete(Long id) {
        secondaryMapper.deleteRelation(id);
        productFirstMapper.deleteByPrimaryKey(id);
    }

    //页面信息对象,根据easyUI的DataGrid组件,返回的数据为json对象,且只有total,rows两个属性
    public PageResult query(QueryObject qo) {
        int count = productFirstMapper.selectCount(qo);
        if (count==0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<ProductFirst> rows = productFirstMapper.queryForList(qo);
        return new PageResult(count,rows);
    }


    public void saveOrUpdate(ProductFirst productFirst) {
        if (productFirst.getId() == null) {

            productFirstMapper.insert(productFirst);
        }else{
            productFirstMapper.updateByPrimaryKey(productFirst);
        }
    }
}
